package cn.iocoder.yudao.module.bpm.controller.admin.hr;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.BpmOALeaveCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.HrCreateReqVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import liquibase.pro.packaged.S;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @Author: 雨同我
 * @Description:
 * @DateTime: 2024/8/15 14:51
 **/

@Tag(name = "管理后台 - Hr工作流申请")
@RestController
@RequestMapping("/bpm/hr")
@Validated
public class HrBpmController {


    @Resource
    private BpmProcessInstanceApi bpmProcessInstanceApi;

    @PostMapping("/create")
    @Operation(summary = "创建hr请求申请")
    public CommonResult<String> createLeave(@Valid @RequestBody HrCreateReqVo createReqVO) {

        // 1. 校验合同是否在审批
//        CrmContractDO contract = validateContractExists(id);
//        if (ObjUtil.notEqual(contract.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus())) {
//            throw exception(CONTRACT_SUBMIT_FAIL_NOT_DRAFT);
//        }

        // 2. 创建合同审批流程实例
        String processInstanceId = bpmProcessInstanceApi.createProcessInstance(createReqVO.getUserId(), new BpmProcessInstanceCreateReqDTO()
                .setProcessDefinitionKey(createReqVO.getKey()).setBusinessKey(String.valueOf(createReqVO.getServiceId())));

        // 3. 更新合同工作流编号
//        contractMapper.updateById(new CrmContractDO().setId(id).setProcessInstanceId(processInstanceId)
//                .setAuditStatus(CrmAuditStatusEnum.PROCESS.getStatus()));

        return CommonResult.success(processInstanceId);
    }
}
